package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.dao.EmployeeInfoMapper;
import com.suyuan.dao.FeedingInforMapper;
import com.suyuan.dao.FeedsMapper;
import com.suyuan.entity.*;
import com.suyuan.service.FeedingService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.vo.FeedingInforVC;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("feedingService")
public class FeedingServiceImpl implements FeedingService {

    @Resource
    private FeedingInforMapper feedingInforMapper;
    @Resource
    private FeedsMapper feedsMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;
    @Resource
    private EmployeeInfoMapper employeeInfoMapper;

    /**
     * 模糊查询
     * @param beginTime
     * @param endTime
     * @param purGid
     * @param page
     * @return
     */
    public PageInfo inquiry(Date beginTime, Date endTime, Integer purGid, Page page) {
        FeedingInforExample feedingInforExample = new FeedingInforExample();
        FeedingInforExample.Criteria criteria = feedingInforExample.createCriteria();
        if(purGid != null){
            criteria.andAnimalidEqualTo(purGid);
        }
        if(beginTime != null){
            criteria.andFeedingtimeGreaterThan(beginTime);
        }
        if(endTime != null){
            criteria.andFeedingtimeLessThan(endTime);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<FeedingInfor> feedingInforList = feedingInforMapper.selectByExample(feedingInforExample);
        PageInfo pageInfo = new PageInfo(feedingInforList);
        List<FeedingInforVC> feedingInforVCList = new ArrayList<FeedingInforVC>();
        for (FeedingInfor feedingInfor: (List<FeedingInfor>)pageInfo.getList()){
            CompanyInfo companyInfo =  companyInfoMapper.selectByPrimaryKey(feedingInfor.getCompid());
            EmployeeInfo employeeInfo = employeeInfoMapper.selectByPrimaryKey(feedingInfor.getEmployeeid());
            Feeds feeds = feedsMapper.selectByPrimaryKey(feedingInfor.getFeedid());
            feedingInforVCList.add(new FeedingInforVC(feedingInfor,employeeInfo,companyInfo,feeds));
        }
        pageInfo.setList(feedingInforVCList);
        return pageInfo;
    }

    /**
     *添加饲养信息
     * @param feedingInfor
     */
    public void addFeedingInfo(FeedingInfor feedingInfor) {
        feedingInforMapper.insert(feedingInfor);
    }


    /**
     * 查找所有饲料信息
     * @return
     */
    public List<Feeds> findAllFeed() {
        FeedsExample feedsExample = null;
        return  feedsMapper.selectByExample(feedsExample);
    }

    /**
     * 删除养殖生猪信息
     * @param selectRows
     * @return
     */
    @Transactional
    public Integer deleteFeedingInfo(String selectRows) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(selectRows);
        for (Integer ID : deleteID){
            feedingInforMapper.deleteByPrimaryKey(ID);
        }
        return 1;
    }



    public Integer deleteByGid(Integer id) {
        feedingInforMapper.deleteByPrimaryKey(id);
        return 1;
    }

    /**
     * 根据饲养信息ID查询所有饲养信息
     * @param gid
     * @return
     */
    public FeedingInfor findByGid(Integer gid) {
         return  feedingInforMapper.selectByPrimaryKey(gid);
    }


    /**
     * 更新饲养信息
     * @param feedingInfor
     */
    public void updateInfo(FeedingInfor feedingInfor) {
        FeedingInfor feedingInfor1 = feedingInforMapper.selectByPrimaryKey(feedingInfor.getFeedingid());
        feedingInfor1.setAreaid(feedingInfor.getAreaid());
        feedingInfor1.setCompid(feedingInfor.getCompid());
        feedingInfor1.setFeedingtime(feedingInfor.getFeedingtime());
        feedingInfor1.setFoodamount(feedingInfor.getFoodamount());
        feedingInfor1.setFoodunit(feedingInfor.getFoodunit());
        feedingInfor1.setRemark(feedingInfor.getRemark());
        feedingInfor1.setEmployeeid(feedingInfor.getEmployeeid());
        feedingInfor1.setFeedid(feedingInfor.getFeedid());
        feedingInforMapper.updateByPrimaryKeySelective(feedingInfor1);
    }



}
